// apiManager.js   定义接口请求以及方法
const apiConfig = {
    // uploadEndpoint: '/api/v3/coll/upload',
    // serverAddress: 'http://8.130.97.204:9000',
    // serverAddress: 'http://192.168.0.103:9000',
    serverAddress: 'http://8.130.114.106:9000',
    uploadEdd: '/api/v1/collection',
    loginEndpoint: '/api/v1/plugin/login', // 登录接口的端点
    // 其他API端点和配置...
};

const MallType = {
    PDD: 1,
    TB: 2,
    TM: 3,
    JD: 4,
    OWN: 5,
    GHPT: 6,
    HYJ: 7,
    ALBB: 8,
    LAZADA: 9,
    JX: 10,
    SHOPEE: 11,
    ALIEXPRESS: 12,
    CrossBorder: 13,
    HYJHW: 15,
    TOKOPEDIA: 16,
    BUKALAPAK: 17,
    DTSHARE: 20
};

function generateRandomClientId() {
    const length = 7; // 目标字符串长度
    const min = Math.pow(10, length - 1); // 生成特定长度数字的最小值
    const max = Math.pow(10, length) - 1; // 生成特定长度数字的最大值
    return Math.floor(Math.random() * (max - min + 1)) + min;
}


//上传数据到云商场
async function uploadData(collData, platformId, userLanguage, key, code, userId, ori_url) {
    const clientId = generateRandomClientId();

    let collKey = platformId + "_" + key + "_" + code;

    //平台编码 商品id 国家编码
    let dataToSend = {
        platform: platformId,
        coll_data: collData, // 使用提取的数据
        coll_key: collKey,
        clientId: clientId,
        language: userLanguage,
        uid: userId,
        ori_url: ori_url,
        uuid: 159,
        vvv: '1.0.0'
    };

    let jsonToSend = JSON.stringify(dataToSend);

    let url = apiConfig.serverAddress + apiConfig.uploadEdd;

    try {
        const response = await fetch(url, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: jsonToSend
        });
        if (!response.ok) {
            throw new Error(`HTTP error! status: ${response.status}`);
        }
        return await response.json();
    } catch (e) {
        console.error('Fetch error:', e);
        throw e;
    }
}

// 登录函数
async function loginUser(username, password, accountType) {
    const url = apiConfig.serverAddress + apiConfig.loginEndpoint; // 构建完整的URL

    try {
        const response = await fetch(url, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ userName: username, userPass: password, accountType: accountType }) // 将用户名和密码作为JSON发送
        });
        if (!response.ok) {
            throw new Error(`HTTP error! status: ${response.status}`);
        }
        return await response.json();
    } catch (e) {
        console.error('登录请求失败:', e);
        throw e;
    }
}

// 默认账户信息
const defaultAccounts = [
    { username: "admin", password: "admin", accountType: "1" },
    { username: "guobin", password: "123456", accountType: "2" },
    { username: "zhou", password: "123456", accountType: "2" },

    // ... 更多账户
];

// async function loginUser(username, password, accountType) {
//     // 模拟成功的响应数据
//     const mockResponse = {
//         code: 200,
//         success: true,
//         message: "登录成功",
//         userData: {
//             username: username,
//             token: "fake-token-123456" // 假设的令牌
//         }
//     };

//     // 模拟网络请求的延时
//     await new Promise(resolve => setTimeout(resolve, 1000));

//     // 返回模拟的响应数据
//     return mockResponse;
// }



